﻿var CurrentArea = {};

Ext.onReady(function () {
    PageSafe();
    ////////////////////////////////////////////////////////////////////////

    var AreaTree = new Ext.tree.TreePanel({
        width: 260,
        style: 'padding-left:20px',
        border: false,
        height: 530,
        split: true,
        rootVisible: true,
        useArrows: false, /*去掉树线*/
        autoScroll: true,
        collapseMode: 'mini',
        root: {},
        listeners: {
            render: function () {
                renderTree(AreaTree); /*渲染菜单树*/
            },
            checkchange: function (node, state) {
                if (state) {
                    CurrentArea = node.attributes;
                    Ext.getCmp("AreaName1").setText(CurrentArea.text);
                    Ext.getCmp("Remark").setText(CurrentArea.AreaRemark);
                }
            }
        }
    });

    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'west',
            style: 'padding:2px;',
            title: '地区列表',
            width: 300,
            iconCls: 'arealist-icon',
            tbar: [{ text: '增加地区', iconCls: 'add-icon', handler: function () {
                var addareawin = new Ext.Window({
                    height: 200,
                    width: 300,
                    title: '增加地区',
                    iconCls: 'areawin-icon',
                    resizable: false,
                    modal: true,
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '父地区',
                        width: 100,
                        readOnly: true,
                        id: 'ParentAddress',
                        name: 'ParentAddress'
                    }, {
                        fieldLabel: '地区名称',
                        width: 100,
                        id: 'AreaName',
                        name: 'AreaName'
                    }, {
                        fieldLabel: '备注',
                        width: 150,
                        id: 'AreaRemark',
                        name: 'AreaRemark'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        var cd = {};
                        if (CurrentArea.id != null) {
                            cd.ParentID = CurrentArea.id;
                        }
                        cd.AreaName = Ext.getCmp("AreaName").getValue();
                        cd.AreaRemark = Ext.getCmp("AreaRemark").getValue();

                        var re = new JsonRequest();
                        re.params.area = cd;
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AddArea'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("操作提示", "地区新增成功");
                                    renderTree(AreaTree);
                                }
                                else {
                                    Ext.Msg.alert("操作提示", "地区新增失败，请联系管理员");
                                }
                                addareawin.close();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { addareawin.close(); } }],
                    listeners: {
                        render: function () {
                            if (CurrentArea.id != null) {
                                Ext.getCmp("ParentAddress").setValue(CurrentArea.text);
                            }
                        }
                    }
                }).show();
            }
            }, { text: '修改地区', iconCls: 'update-icon', handler: function () {
                if (CurrentArea.id != null) {
                    var upwin = new Ext.Window({
                        height: 200,
                        width: 300,
                        title: '修改地区',
                        iconCls: 'areawin-icon',
                        resizable: false,
                        modal: true,
                        layout: 'form',
                        bodyStyle: 'padding: 10px',
                        defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                        labelAlign: 'right',
                        items: [{
                            fieldLabel: '地区名称',
                            width: 100,
                            id: 'u_AreaName',
                            name: 'u_AreaName'
                        }, {
                            fieldLabel: '备注',
                            width: 150,
                            id: 'u_AreaRemark',
                            name: 'u_AreaRemark'
                        }],
                        buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                            var cd = {
                                PID: CurrentArea.id,
                                ParentID: CurrentArea.parentId,
                                AreaName:Ext.getCmp("u_AreaName").getValue(),
                                AreaRemark:Ext.getCmp("u_AreaRemark").getValue()
                            };

                            var re = new JsonRequest();
                            re.params.area = cd;
                            Ext.Ajax.request({
                                url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'UpdateArea'),
                                jsonData: re,
                                success: function (re, op) {
                                    var obj = Ext.decode(re.responseText);
                                    if (obj.ResponseObject != null) {
                                        Ext.Msg.alert("操作提示", "地区修改成功");
                                        renderTree(AreaTree);
                                    }
                                    else {
                                        Ext.Msg.alert("操作提示", "地区修改失败，请联系管理员");
                                    }
                                    upwin.close();
                                }
                            });
                        }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }],
                        listeners: {
                            render: function () {
                                if (CurrentArea.id != null) {
                                    Ext.getCmp("u_AreaName").setValue(CurrentArea.text);
                                    Ext.getCmp("u_AreaRemark").setValue(CurrentArea.AreaRemark);
                                }
                            }
                        }
                    }).show();
                }
                else {
                    Ext.Msg.alert("缺少项提示", "请选择一个需要修改的地区");
                }
            }
            }, { text: '删除地区', iconCls: 'del-icon', handler: function () {
                if (CurrentArea.id != null) {
                    var re = new JsonRequest();
                    re.params.PID = CurrentArea.id;
                    Ext.Ajax.request({
                        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'DelArea'),
                        jsonData: re,
                        success: function (re, op) {
                            var obj = Ext.decode(re.responseText);
                            if (obj.ResponseObject != null) {
                                Ext.Msg.alert("操作提示", "地区删除成功");
                                renderTree(AreaTree);
                            }
                            else {
                                Ext.Msg.alert("操作提示", "地区删除失败，请联系管理员");
                            }
                        }
                    });
                }
                else {
                    Ext.Msg.alert("缺少项提示", "请选择一个需要删除的地区");
                }
            }
            }],
            items: AreaTree
        }, {
            region: 'center',
            style: 'padding:2px;',
            title: '地区详细信息',
            layout: 'table',
            bodyStyle: 'padding: 50px',
            layoutConfig: {
                columns: 2,
                tableAttrs: {
                    height: 200
                }
            },
            items: [{
                xtype: 'label',
                text: '地区名称：'
            }, {
                xtype: 'label',
                id: 'AreaName1',
                name: 'AreaName1'
            }, {
                xtype: 'label',
                text: '备注：'
            }, {
                xtype: 'label',
                id: 'Remark',
                name: 'Remark'
            }]
        }]
    });
});

//初始化树
var renderTree = function (tree) {
    var re = new JsonRequest();
    re.category = CATEGORY.READONLY;
    re.params.param = {};
    Ext.Ajax.request({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AreaData'),
        jsonData: re,
        success: function (re, op) {
            var obj = Ext.decode(re.responseText).ResponseObject;
            var ns = {}, f = tree;
            ns.Children = obj;
            var r = new Ext.tree.TreeNode({
                id: '0',
                text: '全部地区'
            });
            var selectedNode = null, selectedId = f.selectedId;
            function appendChild(node, o) {
                if (node.PID == selectedId) {
                    selectedNode = node;
                }
                if (o.Children != null && o.Children.length > 0) {
                    for (var a = 0; a < o.Children.length; a++) {
                        var n = new Ext.tree.TreeNode({
                            id: o.Children[a].PID,
                            icon: '../images/menu/ui-text-area.png',
                            text: o.Children[a].AreaName,
                            parentId: o.Children[a].ParentID,
                            AreaRemark: o.Children[a].AreaRemark,
                            checked: false
                        });
                        node.appendChild(n);
                        appendChild(n, o.Children[a]);

                    }
                }
            }
            appendChild(r, ns);
            f.setRootNode(r);

            tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
            tree.root.expand(true);
        }
    })
}